package com.neusoft.servlet;

import com.alibaba.fastjson.JSON;
import com.neusoft.dto.UserDto;
import com.neusoft.entity.UsersEntity;
import com.neusoft.mapper.TagMapper;
import com.neusoft.mapper.UsersMapper;
import com.neusoft.model.UserInfo;
import com.neusoft.utils.SqlSessionFactoryUtils;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

@WebServlet("/users")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonReq = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8);
        System.out.println(jsonReq);
        UserDto userDto = JSON.parseObject(jsonReq, UserDto.class);
        userDto.getUser().setBio("day day up");
        userDto.getUser().setImage("abc.jpg");

        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
//        Integer effected_rows = usersMapper.registerUser(userDto.getUser());
        System.out.println(userDto.getUser().getId());

        sqlSession.commit();
        sqlSession.close();

        PrintWriter printWriter = resp.getWriter();
//        printWriter.println(effected_rows);
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String jsonReq = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8);
        UserDto userDto = JSON.parseObject(jsonReq, UserDto.class);

        userDto.getUser().setId(19L);

        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);

        Integer effected_rows = usersMapper.updateUser(userDto.getUser());


        sqlSession.commit();
        sqlSession.close();

        PrintWriter printWriter = resp.getWriter();
        printWriter.println(effected_rows);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        SqlSessionFactory sqlSessionFactory
                = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        UsersEntity usersEntity = usersMapper.getUserById(19L);

        UserDto userDto = new UserDto();
        userDto.setUser(usersEntity);

        String jsonStr = JSON.toJSONString(userDto);

        PrintWriter printWriter = resp.getWriter();
        printWriter.println(jsonStr);
    }
}
